# Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
# Licensed under the 【火山方舟】原型应用软件自用许可协议
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#     https://www.volcengine.com/docs/82379/1433703
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# AI 写文章，客户端，调用server服务端
# 前提：需要打开 server.py ,localhost:8888

from openai import OpenAI

client = OpenAI(base_url="http://localhost:8888/api/v3/bots", api_key="{API_KEY}")


def main():

    QUERY1 = """你是一个资深积木乐高专栏作家，写作流程：
1.基于以下新闻，请找出1个和新闻相关的主题（不要关心品牌，年份等细节，主题应该是简单的5个字以内），然后基于每一个新闻主题整理一个图文介绍（包括展示"image_arr_new"图片)。例如：新闻"三叉戟的八格车来袭！玛莎拉蒂授权积木实测：咬合力拉满，鲨鱼腮却失踪了？【测评】" ，那么主题就是“八格车”“莎拉蒂”。
2.然后进行至少一次搜索：搜索相关新闻主题进行资料补充（例如，怎样拼装[主题],查找[主题]等等），补充资料也需要图片展示 ，同时整理资料不要复杂（简单补充则可，资料相关就可以，搜集资料能够写出文章就可以，同时每一个主题至少搜集一次补充资料）
3.最后：按以上整理的所有内容写一个吸引人的文章【描写热点主题相关能够让观众感到兴趣和愉快】，文章内容包括今天热点主题介绍+补充的相关知识资料，文章主题结构：【主题1+补充1，主题2+补充2，...】
4.注意：务必要保留搜索到的相关图片，分析主题不要过多关注（品牌，定价，用户评价，具体细节）。
5.现在给你提供今天小红书的积木热点新闻材料：###  

   {
      "_id": "68353ff0ba2d733faeefddd9",
      "note_id": "679dd36b000000001801a602",
      "title": "乐高MOC图纸：小米SU7",
      "user_nickname": "慕舞歆",
      "user_avatar": "https://sns-avatar-qc.xhscdn.com/avatar/1040g2jo315vum3482o6g5nhd7fl08ogk8v769bg?imageView2/2/w/80/format/jpg",
      "user_id": "5e2d3bea0000000001006214",
      "liked_count": "1075",
      "collected_count": "972",
      "comment_count": "89",
      "shared_count": "82",
      "cover_url": "http://sns-webpic-qc.xhscdn.com/202505271229/bad434ce664abbffade7ce08cafcfbd7/1040g2sg31hpdicnjjuk05nhd7fl08ogku9uitmo!nc_n_webp_mw_1",
      "publish_time": 1748320209,
      "image_arr_new": [
        "https://legoallbricks.oss-cn-shenzhen.aliyuncs.com/xhs_images/1040g2sg31hpdicnjjuk05nhd7fl08ogku9uitmo!nc_n_webp_prv_1.jpg",
        "https://legoallbricks.oss-cn-shenzhen.aliyuncs.com/xhs_images/1040g2sg31hpdicnjjuj05nhd7fl08ogkacmkrfo!nc_n_webp_prv_1.jpg",
        "https://legoallbricks.oss-cn-shenzhen.aliyuncs.com/xhs_images/1040g00831hpdkk183o6g5nhd7fl08ogk08tdjv0!nc_n_webp_prv_1.jpg",
        "https://legoallbricks.oss-cn-shenzhen.aliyuncs.com/xhs_images/1040g2sg31hpdkk17jukg5nhd7fl08ogk4djk138!nc_n_webp_prv_1.jpg",
        "https://legoallbricks.oss-cn-shenzhen.aliyuncs.com/xhs_images/1040g00831hpdkk17js6g5nhd7fl08ogkgcvvm20!nc_n_webp_prv_1.jpg",
            ],
      "synced": false
    },
    

 ###，"""
    # stream run
    stream_resp = client.chat.completions.create(
        model="test",
        messages=[
            {
                "role": "user",
                "content": QUERY1,
            }
        ],
        stream=True,
    )

    thinking = False

    for chunk in stream_resp:
        if chunk.choices[0].delta.model_extra.get("reasoning_content"):
            if not thinking:
                print("\n----思考过程----\n")
                thinking = True
            content = chunk.choices[0].delta.model_extra.get("reasoning_content", "")
            print(content, end="", flush=True)
        elif chunk.choices[0].delta.content:
            if thinking:
                print("\n----输出回答----\n")
                thinking = False
            print(chunk.choices[0].delta.content, end="", flush=True)


if __name__ == "__main__":
    main()
